// AI服务接口
const API_BASE_URL = 'https://api.deepseek.com/v1';
const API_KEY = 'sk-bc5601d815794abe8e679d4714e9fb3e';
const TIMEOUT = 30000; // 30秒超时

export const getAIRecommendation = async (prompt, menuItems, season) => {
  try {
    const response = await uni.request({
      url: `${API_BASE_URL}/chat/completions`,
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${API_KEY}`
      },
      timeout: TIMEOUT,
      data: {
        model: 'deepseek-chat',
        messages: [
          {
            role: 'system',
            content: '你是一个专业的奶茶推荐助手，根据用户需求和当前季节推荐合适的奶茶。请简洁明了地回答，并突出推荐的具体奶茶名称。'
          },
          {
            role: 'user',
            content: `当前是${season}季节，可选的奶茶有：${menuItems.join('、')}。${prompt}`
          }
        ],
        temperature: 0.7,
        max_tokens: 1000
      }
    });
    
    const [err, res] = response;
    if (err) {
      throw err;
    }
    
    if (!res.data || !res.data.choices || !res.data.choices[0]) {
      throw new Error('AI返回数据格式错误');
    }
    
    return res.data.choices[0].message.content;
  } catch (error) {
    console.error('AI推荐请求失败:', error);
    if (error.errMsg && error.errMsg.includes('timeout')) {
      throw new Error('请求超时，请稍后重试');
    }
    throw error;
  }
};